package com.enjoyor.soa.traffic.rest.efoms.controller;

import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.properties.FileHanderProperties;
import com.enjoyor.soa.traffic.frame.support.file.IFilesTool;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Auth(excludes = {"downloadFile"})
@RestController
@RequestMapping("/file")
@Api("文件处理api")
@CrossOrigin
public class FileInfoController {

    @Autowired
    FileHanderProperties fileHanderProperties;

    @Autowired
    private IFilesTool filesTool;



    @PostMapping(value = { "/uploadFile" }, consumes = "multipart/*", produces="application/json;charset=UTF-8")
    @ApiOperation(value = "上传文件", notes = "上传文件")
    public ResultPojo uploadFile(@RequestParam(value = "file") MultipartFile file) {
    	System.out.println("1--------------------------"+file.getOriginalFilename()+"---------------------");
        ResultPojo resultPojo = filesTool.fileUpload(file);
        Map<String, String> map = (HashMap<String, String>)resultPojo.getResultList();
        String mappingAddress = map.get("mappingAddress");
        String addr = mappingAddress.substring(0,mappingAddress.lastIndexOf("fileResource"));
        //文件下载地址（图片以及文件）
        String downloadPath = addr+"file/downloadFile?secondDir="+map.get("secondDir")+"&fileName="+map.get("fileName");
        map.put("downloadPath",downloadPath);
        resultPojo.setResultList(map);
        System.out.println("2--------------------------"+map.get("fileName")+"---------------------");
        return resultPojo;
        
    }

    @GetMapping("/downloadFile")
    @ApiOperation(value = "下载文件", notes = "下载文件")
    @ApiImplicitParams ({
    @ApiImplicitParam(name = "secondDir", value = "二级目录名", paramType = "query", dataType = "String"),
    @ApiImplicitParam(name = "fileName", value = "文件全名", paramType = "query", dataType = "String")})
    public void downloadFile(@RequestParam(value = "secondDir") String secondDir,
                             @RequestParam(value = "fileName") String fileName, HttpServletResponse response) {
        filesTool.downloadFile(secondDir, fileName, response);
    }

    @DeleteMapping("/deleteFile")
    @ApiOperation(value = "删除文件", notes = "删除文件")
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "secondDir", value = "二级目录名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "fileName", value = "文件全名", paramType = "query", dataType = "String")})
    public void deleteFile(@RequestParam(value = "secondDir") String secondDir,
                             @RequestParam(value = "fileName") String fileName, HttpServletResponse response) {
        filesTool.removeFile(secondDir, fileName);
    }

}
